package com.hepinghospital.user.ui.activity.graphic.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.hepinghospital.user.R
import com.hepinghospital.user.base.BaseDialogFragment
import com.hepinghospital.user.ui.activity.userinfo.MyFamilyActivity
import com.hepinghospital.user.util.ToastUtil
import com.rest.business.RestProxy
import com.rest.observer.BaseObserver
import jz.joyoung.robot.util.LogUtil

/**
 * @Description 查询就诊记录状态弹窗
 * @Author WuJianFeng
 * @Date 2022/8/18 14:00
 */
/**
 * MedicalRecordsDialog状态
 * 0 : 查询中
 * 1 : 查询成功
 * 2 : 查询失败
 */
const val MedicalRecordsDialog_STATE_0 = 0
const val MedicalRecordsDialog_STATE_1 = 1
const val MedicalRecordsDialog_STATE_2 = 2
class MedicalRecordsDialog private constructor(): BaseDialogFragment(){


    lateinit var stateIv : ImageView
    lateinit var stateTv : TextView
    lateinit var stateContentTv : TextView
    lateinit var stateBtn : TextView
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) //设置透明背景
        val view: View = inflater.inflate(R.layout.dialog_medical_records, container)
        stateIv = view.findViewById(R.id.state_iv)
        stateTv = view.findViewById(R.id.state_tv)
        stateContentTv = view.findViewById(R.id.state_content_tv)
        stateBtn = view.findViewById(R.id.state_btn)
        return view
    }

    private fun resizeDialogFragment() {
        if (null != dialog) {
            val window = dialog!!.window
            val lp = dialog!!.window!!.attributes
            lp.width =  (context?.resources?.getDimension(R.dimen.dp250))!!.toInt()// 屏幕宽度（像素）
            lp.height = (context?.resources?.getDimension(R.dimen.dp320))!!.toInt() // 屏幕高度（像素）
            window?.setLayout(lp.width, lp.height)
        }
    }

    companion object {
        val instance: MedicalRecordsDialog by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { MedicalRecordsDialog() }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        changeDialogState(MedicalRecordsDialog_STATE_0)
        patientMedicalRecords()

        stateBtn.setOnClickListener {
            when(stateBtn.text){
                getString(R.string.string_cancel) -> {
                    dismissAllowingStateLoss()
                }
                getString(R.string.state_query_succeed_btn) -> {

                    if (context is MyFamilyActivity){
                        (context as MyFamilyActivity).startPayActivity()
                    }
                }

                getString(R.string.string_return) -> {
                    if (context is MyFamilyActivity){
                        (context as MyFamilyActivity).finish()
                    }
                }

            }

            dismissAllowingStateLoss()
        }
    }

    private fun patientMedicalRecords(){
        RestProxy.getInstance().patientMedicalRecords(if(context is MyFamilyActivity)(context as MyFamilyActivity).patientId else "", object : BaseObserver<Boolean>() {

            override fun onError(e: Throwable) {
                changeDialogState(MedicalRecordsDialog_STATE_2)
                ToastUtil.showToast(context,e.message)
            }

            override fun onSuccess(result: Boolean) {
                if (result){
                    changeDialogState(MedicalRecordsDialog_STATE_1)
                }else{
                    changeDialogState(MedicalRecordsDialog_STATE_2)
                }
            }


        })
    }


    /**
     * dialog状态
     */
    private fun changeDialogState(state : Int){

        if (!dialog!!.isShowing && state != MedicalRecordsDialog_STATE_0){
            return
        }

        when(state){
            MedicalRecordsDialog_STATE_0 -> {
                stateIv.setImageDrawable(ContextCompat.getDrawable(mContext,R.drawable.icon_querying))
                stateTv.text = getString(R.string.state_querying)
                stateContentTv.text = getString(R.string.state_querying_content)
                stateBtn.text = getString(R.string.string_cancel)
            }
            MedicalRecordsDialog_STATE_1 -> {
                stateIv.setImageDrawable(ContextCompat.getDrawable(mContext,R.drawable.icon_query_success))
                stateTv.text = getString(R.string.state_query_succeed)
                stateContentTv.text = getString(R.string.state_query_succeed_content)
                stateBtn.text = getString(R.string.state_query_succeed_btn)
            }
            MedicalRecordsDialog_STATE_2 -> {
                stateIv.setImageDrawable(ContextCompat.getDrawable(mContext,R.drawable.icon_query_fail))
                stateTv.text = getString(R.string.state_query_failed)
                stateContentTv.text = getString(R.string.state_query_failed_content)
                stateBtn.text = getString(R.string.string_return)
            }
        }
    }

}